package com.ktwlsoft.archivemanage.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ktwlsoft.archivemanage.domain.business.businessService.DepartmentManageService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.OperationRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.Department;
import com.ktwlsoft.archivemanage.domain.dao.entity.EditHistory;
import com.ktwlsoft.archivemanage.domain.dao.entity.OperationLog;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.department.DepartmentManageSaveRequest;
import com.ktwlsoft.archivemanage.payload.department.DepartmentManageUpdateRequest;
import com.ktwlsoft.archivemanage.payload.operationLog.SaveEditHistoryRequest;
import com.ktwlsoft.archivemanage.payload.operationLog.SaveLogRequest;
import com.ktwlsoft.archivemanage.service.security.UserAuthenticate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 部门管理
 *
 * @author zhuhe on 2019/10/28 9:27
 * @version 1.0
 */
@Api(value = "部门管理", description = "部门管理")
@RestController
@RequestMapping("departmentManage")
public class DepartmentManageController {

    /**部门业务*/
    private final DepartmentManageService departmentManageService;

    public DepartmentManageController(DepartmentManageService departmentManageService) {
        this.departmentManageService = departmentManageService;
    }

    /**
      * 部门管理列表 on 2019/10/28 9:32
      *
      * @param state state
      * @return ResponseEntity<?>
      */
    @ApiOperation(value = "部门管理列表", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("findPage")
    @UserAuthenticate(actionCode = "DEPARTMENT_PAGE", skip = true)
    public ResponseEntity<?> findPage(@RequestParam(defaultValue = "1") int pn,
                                      @RequestParam(defaultValue = "30") int pageSize,
                                      @ApiParam(name = "state", value = "状态") @RequestParam(defaultValue = "", required = false) Department.State state){
        return ResponseEntity.ok(departmentManageService.findPage(pn,pageSize,state));
    }

    /**
     * 部门管理新增 on 2019/10/28 9:32
     *
     * @param request request
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "部门管理新增", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping("save")
    @UserAuthenticate(actionCode = "DEPARTMENT_ADD")
    public ResponseEntity<?> save(@Valid @RequestBody DepartmentManageSaveRequest request) {
        Result result = departmentManageService.save(request);
        if(result.getResultType().equals(ResultType.BUSINESS_SUCCESS)){
            saveLog(OperationLog.OperationType.ADD,"添加部门《" + request.getDepartment() + "》");
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 部门管理修改 on 2019/10/28 9:32
     *
     * @param request request
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "部门管理修改", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @PostMapping("modify")
    @UserAuthenticate(actionCode = "DEPARTMENT_EDIT")
    public ResponseEntity<?> modify(@Valid @RequestBody DepartmentManageUpdateRequest request) {
        Result result = departmentManageService.modify(request);
        if(result.getResultType().equals(ResultType.BUSINESS_SUCCESS)){
            SaveEditHistoryRequest historyRequest = new SaveEditHistoryRequest();
            historyRequest.setOldEntity(request.getOldValue());
            historyRequest.setNewEntity(request.getNewValue());
            historyRequest.setBusinessId(request.getId());
            historyRequest.setBusinessFrom(EditHistory.BusinessFrom.EDIT_DEPARTMENT);
            saveLog(result,OperationLog.OperationType.EDIT,"编辑部门《" + request.getDepartment() + "》",historyRequest);
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 部门管理删除 on 2019/10/28 9:32
     *
     * @param id id
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "部门管理删除", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
    @ResponseBody
    @GetMapping("deleteById")
    @UserAuthenticate(actionCode = "DEPARTMENT_DELETE")
    public ResponseEntity<?> deleteById(@ApiParam(name = "id", value = "部门id") @RequestParam(defaultValue = "", required = false) Long id){
        Result result = departmentManageService.deleteById(id);
        if(result.getResultType().equals(ResultType.BUSINESS_SUCCESS)){
            saveLog(OperationLog.OperationType.DELETE,"删除部门《" + result.getData() + "》");
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 新增日志 on 2019/11/14 10:37
     *
     * @param operationType 操作类型
     * @param description 描述
     */
    private void saveLog(OperationLog.OperationType operationType, String description){
        OperationRepositoryService.saveLog(new SaveLogRequest(
                operationType,
                description,
                "部门管理",
                OperationLog.BusinessType.SYSTEM
        ));
    }

    /**
     * 新增日志 on 2019/11/14 10:37
     *
     * @param operationType 操作类型
     * @param description 描述
     */
    private void saveLog(Result result,OperationLog.OperationType operationType, String description,SaveEditHistoryRequest editHistoryRequest){
        OperationRepositoryService.saveLog(result,new SaveLogRequest(
                operationType,
                description,
                "部门管理",
                OperationLog.BusinessType.SYSTEM
        ),editHistoryRequest);
    }
}